﻿using NewfarTormentTool.core;
using NewfarTormentTool.data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewfarTormentTool.sub
{
    public partial class ImportUser : Form
    {
        public ImportUser()
        {
            InitializeComponent();
            Label_ErrorTips.Visible = false;
            Label_ErrorTips.BindingContextChanged += new EventHandler(Label_ErrorTips_Changed);
            Label_ErrorFile.Visible = false;
            Label_ErrorFile.BindingContextChanged += new EventHandler(Label_ErrorFile_Changed);
        }

        /// <summary>
        /// 重设报错标签位置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Label_ErrorTips_Changed(object sender, EventArgs e)
        {
            int x = Width - Label_ErrorTips.Width - 30;
            Label_ErrorTips.Location = new Point(x, Label_ErrorTips.Location.Y);
        }

        /// <summary>
        /// 重设报错标签位置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Label_ErrorFile_Changed(object sender, EventArgs e)
        {
            int x = Width - Label_ErrorFile.Width - 30;
            Label_ErrorFile.Location = new Point(x, Label_ErrorFile.Location.Y);
        }


        /// <summary>
        /// 主面板
        /// </summary>
        private MainForm Main;

        /// <summary>
        /// 设置主面板
        /// </summary>
        public void SetMain(MainForm mf)
        {
            Main = mf;
        }

        /// <summary>
        /// 选择导入文件
        /// </summary>
        private void Btn_ChooseFile_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                // 设置初始目录
                dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                // 设置文件过滤器
                dialog.Filter = "excel files(*.xlsx)|*.xlsx";
                dialog.FilterIndex = 2;
                dialog.RestoreDirectory = true;
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    // 获取选定文件的路径
                    string filePath = dialog.FileName;
                    TB_File.Text = filePath;
                }
            }
        }

        /// <summary>
        /// 导入
        /// </summary>
        private void Btn_Import_Click(object sender, EventArgs e)
        {
            string user = TB_User.Text;
            if (string.IsNullOrEmpty(user) || "$".Equals(user))
            {
                Label_ErrorTips.Text = "请输入正确的用户名!";
                Label_ErrorTips.Visible = true;
                return;
            }
            else
            {
                Label_ErrorTips.Visible = false;
            }
            string excelFileName = TB_File.Text;
            UserRecord record = UserRecord.LoadFromFile(excelFileName, user);
            if (record == null)
            {
                Label_ErrorFile.Text = "文件解析失败!";
                Label_ErrorFile.Visible = true;
                return;
            }
            else
            {
                Label_ErrorFile.Visible = false;
            }
            if (RoleDataStorage.Instance.Records.ContainsKey(user))
            {
                if (MessageBox.Show("该用户已存在,是否覆盖记录?", "Warn", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
                {
                    return;
                }
            }
            RoleDataStorage.Instance.AddOrUpdateRecord(user, record);
            // 指定新的当前用户
            RoleDataStorage.Instance.CurUser = user;
            RoleDataStorage.Instance.LoadUserRoles(user);
            // 主面板刷新
            Main.RefreshCombo();
            Main.RefreshSaveButton();
            Main.RefreshMain();
            MessageBox.Show("导入完成!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            // 移除自己
            Close();
        }
    }
}
